package com.leyou.item.controller;

import com.leyou.common.pojo.PageResult;
import com.leyou.item.pojo.Brand;
import com.leyou.item.pojo.Category;
import com.leyou.item.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("brand")
public class BrandController {

    @Autowired
    private BrandService brandService;

    @GetMapping("page")
    public ResponseEntity<PageResult<Brand>> queryBrandByPage(
            @RequestParam(value = "page",defaultValue = "1") Integer page,
            @RequestParam(value = "rows",defaultValue = "5") Integer rows,
            @RequestParam(value = "sortBy",required = false) String sortBy,
            @RequestParam(value = "desc",defaultValue = "false")Boolean desc,
            @RequestParam(value = "key",required = false) String key
    ){
        PageResult<Brand> pageResult = this.brandService.queryBrandByPageAndSort(page, rows, sortBy, desc, key);
        if(pageResult.getItems() == null || pageResult.getItems().size()==0){
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
        return ResponseEntity.ok(pageResult);
    }

    @PostMapping
    public ResponseEntity<Brand> saveBrand(Brand brand, @RequestParam("cids")List<Long>cids){

        brandService.saveBrand(brand,cids);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @PutMapping
    public ResponseEntity<String> updateBrand(Brand brand,@RequestParam("cids")List<Long> cids){
        this.brandService.updateBrand(brand,cids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @DeleteMapping("{id}")
    public ResponseEntity<String> deleteByID(@PathVariable("id") Long id){
        brandService.deleteBrandById(id);
        return ResponseEntity.ok(null);
    }

    @GetMapping("cid/{cid}")
    public ResponseEntity<List<Brand>> queryBrandByCid(@PathVariable("cid")Long cid){
        List<Brand> brands = this.brandService.queryBrandByCid(cid);
        if(brands.size()==0){
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
        return ResponseEntity.ok(brands);
    }

    @GetMapping("bid/{bid}")
    public Brand queryBrandByBid(@PathVariable("bid")Long bid){
        Brand brand = this.brandService.queryBrandByBid(bid);
        return brand;
    }

    @GetMapping("list")
    public ResponseEntity<List<Brand>> queryBrandListByIds(@RequestParam("ids")List<Long> ids){
        List<Brand> brands = this.brandService.queryBrandsByIds(ids);
        if(brands==null||brands.size()==0){
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
        return ResponseEntity.ok(brands);
    }


}
